﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DataLoad.iContact.WinClient
{
    public class NameParser
    {
        private string _title;

        public string Title
        {
            get
            {
                return _title;
            }
        }

        private string _firstName;

        public string FirstName
        {
            get
            {
                return _firstName;
            }
        }

        private string _middleName;

        public string MiddleName
        {
            get
            {
                return _middleName;
            }
        }

        private string _lastName;

        public string LastName
        {
            get
            {
                return _lastName;
            }
        }

        private string _pedigree;

        public string Pedigree
        {
            get
            {
                return _pedigree;
            }
        }

        private string _degree;

        public string Degree
        {
            get
            {
                return _degree;
            }
        }

        private string[] _titles = new string[] {"Mr.", "Mrs.", "Ms.", "Dr.", "Miss", "Sir" , "Madam", "Mayor", "President"};

        private string[] _pedigrees = new string[] {"Jr.", "Sr.", "III", "IV", "VIII", "IX", "XIII"};

        private bool IsIn(string text, string[] values, bool caseSensitive)
        {
            foreach (string v in values)
            {
                StringComparison compType = StringComparison.InvariantCulture;

                if (!caseSensitive)
                {
                    compType = StringComparison.InvariantCultureIgnoreCase;
                }

                if (text.Equals(v, compType))
                {
                    return true;
                }
            }

            return false;
        }

        private string CutWord(string text, out string remainder)
        {
            int p = text.IndexOf(' ');

            if (p != -1)
            {
                remainder = text.Substring(p).Trim();
                return text.Substring(0, p).Trim();
            }

            remainder = string.Empty;
            return text;
        }

        private string CutLastWord(string text, out string remainder)
        {
            int p = text.LastIndexOf(' ');

            if (p != -1)
            {
                remainder = text.Substring(0, p).Trim();
                return text.Substring(p).Trim();
            }

            remainder = string.Empty;
            return text;
        }

        public NameParser(string text)
        {
            // title
            string word = CutWord(text, out text);
            if (IsIn(word, _titles, false))
            {
                _title = word;
            }
            else
            {
                text = string.Format("{0} {1}", word, text);
            }

            // degree
            int p = text.IndexOf(',');
            if (p != -1)
            {
                _degree = text.Substring(p + 1).Trim();
                text = text.Substring(0, p).Trim();
            }

            // pedigree
            word = CutWord(text, out text);
            if (IsIn(word, _pedigrees, false))
            {
                _pedigree = word;
            }
            else
            {
                text = string.Format("{0} {1}", word, text);
            }
            
            // rest
            _lastName = CutLastWord(text, out text);
            _firstName = CutWord(text, out text);
            _middleName = text.Trim();
        }
    }
}